/* eslint-disable no-return-assign */
/* eslint-disable camelcase */
import Vue from 'vue'
import Vuex from 'vuex'
import { getCart } from '@/api/order'

Vue.use(Vuex)

// 数据存储状态
const state = {
  count: 10,
  cart_lists: []
}
// 同步的mutations使用，this.$store.commit()
const mutations = {
      mutationAdd(state, n = 0) {
          console.log('mutationAdd')
          return state.count += n // 返回运算结果
      },
      mutationReduce(state, n = 0) {
          console.log('mutationReduce')
          return state.count -= n
    },
      saveUser(state, value) {
        state.userinfo = value
        localStorage.setItem('userinfo', value)
      },
    delUser(state) {
    state.userinfo = null
    localStorage['userinfo'] = null
    localStorage['token'] = null
    state.cart_lists = []
    localStorage['cart_lists'] = null
  },
      saveCart(state) {
    getCart().then((response) => {
      state.cart_lists = response.data.data
      console.log('data ' + JSON.stringify(response.data))
      // var totalprice = 0;
      // var totalnum = 0;
      // console.log(state.cart_lists.goods_list)
      // for (var i = 0; i < state.cart_lists.goods_list.length; i++) {
      //  var item = state.cart_lists.goods_list[i]
      //  console.log("item" + item)
      //  totalprice += item.goods_num * item.goods.price;
      //  totalnum += item.goods_num
      // }
      // state.cart_lists.total_price = totalprice
      // state.cart_lists.total_num = totalnum
      localStorage.setItem('cart_lists', JSON.stringify(state.cart_lists))
    }).catch(function (error) {
      console.log(error)
    })
  }
}

// 异步actions使用，this.$store.dispatch()
const actions = {
    actionsAdd(context, n = 0) {
      console.log('actionsAdd')
      return context.commit('mutationAdd', n)
  },
    actionsReduce(context, n = 0) {
      console.log('actionsReduce')
      return context.commit('mutationReduce', n)
  },
  saveUser(context, value) {
      return context.commit('saveUser', value)
    },
    delUser(context) {
      return context.commit('delUser')
    },
    saveCart(context) {
      return context.commit('saveCart')
    }
}

const getters = {
  getCount(state) {
    console.log('state.count数据变化了,执行了方法 getCount')
    return state.count
  },
  userinfo(state) {
    localStorage.setItem('userinfo', JSON.stringify(state.userinfo))
    return state.userinfo
  },
  cart_lists(state) {
    // localStorage.setItem("cart_lists", JSON.stringify(state.cart_lists));
    return state.cart_lists
  },
  totalNum(state) {
    let goods_num = 0
    if (state.cart_lists.length > 0) {
      state.cart_lists.forEach((item) => {
        goods_num += item.goods_num
      })
    }
    return goods_num
  },
  totalPrice(state) {
    let price = 0
    if (state.cart_lists.length > 0) {
      state.cart_lists.forEach((item) => {
        price += item.goods.price * item.goods_num
      })
    }
    return price
  }
}

const store = new Vuex.Store({
  state,
  mutations,
  actions,
  getters
})

export default store
